package com.doctor.windflower_doctor.fragment;

import android.content.Intent;
import android.graphics.Bitmap;
import android.media.Image;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.doctor.windflower_doctor.R;
import com.doctor.windflower_doctor.SystemApplication;
import com.doctor.windflower_doctor.actionBeen.DoctorAction;
import com.doctor.windflower_doctor.activity.MessageActivity;
import com.doctor.windflower_doctor.activity.QrCodeActiviy;
import com.doctor.windflower_doctor.activity.SetUserActivity;
import com.doctor.windflower_doctor.db.SharedPreferencesDB;
import com.doctor.windflower_doctor.entity.DoctorBeen;
import com.doctor.windflower_doctor.fragment.MenuFragment.SLMenuListOnItemClickListener;
import com.doctor.windflower_doctor.http.callback.HttpRequestCallBack;
import com.doctor.windflower_doctor.http.parser.impl.JsonParser;
import com.doctor.windflower_doctor.http.request.HttpRequest;
import com.doctor.windflower_doctor.http.request.HttpRequestParams;
import com.doctor.windflower_doctor.http.response.HttpResponseInfo;
import com.doctor.windflower_doctor.util.Constent;
import com.doctor.windflower_doctor.util.LogUtils;
import com.doctor.windflower_doctor.view.RoundedImageView;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.client.HttpRequest.HttpMethod;
import com.nostra13.universalimageloader.core.DisplayImageOptions;

public class UserFragment extends BaseFragment implements
		SLMenuListOnItemClickListener {
	private RoundedImageView iv_user;
	private LinearLayout tv_set, tv_sign;
	private TextView tv_name, tv_close, tv_no, tv_good;
	private RelativeLayout relativeLayout1, relativeLayout2, rightlayout;
	private String qr, name, imgurl;
	private static UserFragment userFragment;
	private DisplayImageOptions options;
	private boolean stopService;
	private ImageView red;

	public static UserFragment getInstance(Bundle bundle) {
		if (userFragment == null) {
			userFragment = new UserFragment();
			userFragment.setArguments(bundle);
		}
		return userFragment;
	}

	@Override
	protected View setConentView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		return inflater.inflate(R.layout.activity_user, container, false);
	}

	@Override
	protected void getSaveData(Bundle savedInstanceState) {
		// TODO Auto-generated method stub

	}

	@Override
	protected void processLogic() {
		// TODO Auto-generated method stub

	}

	@Override
	public void onStart() {
		// TODO Auto-generated method stub
		super.onStart();
		getUser();
	}

	@Override
	protected void setListener() {
		// TODO Auto-generated method stub
		relativeLayout2.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				LogUtils.e("=================relativeLayout2=================");
				Intent intent = new Intent(
						MenuFragment.ACTION_FRAGMENT_MYQUESTIONFRAGMENT);
				intent.putExtra("type", 1);
				getActivity().sendBroadcast(intent);
			}
		});
		relativeLayout1.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				LogUtils.e("=================relativeLayout1=================");
				Intent intent = new Intent(
						MenuFragment.ACTION_FRAGMENT_MYQUESTIONFRAGMENT);
				intent.putExtra("type", 3);
				getActivity().sendBroadcast(intent);
			}
		});
		tv_set.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				Intent intent = new Intent(getActivity(), SetUserActivity.class);
				intent.putExtra("imgurl", imgurl);
				intent.putExtra("stopService", stopService);
				startActivity(intent);
			}
		});
		tv_sign.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				// popupWindow_ewm.showAtLocation(relativeLayout,
				// Gravity.CENTER, 0, 0);
				Intent intent = new Intent(getActivity(), QrCodeActiviy.class);
				intent.putExtra("name", name);
				intent.putExtra("img", qr);
				intent.putExtra("imgurl", imgurl);
				startActivity(intent);

			}
		});

	}

	@Override
	protected void findViews(View view) {
		// TODO Auto-generated method stub
		red = (ImageView) getActivity().findViewById(R.id.iv_ti);
		tv_set = (LinearLayout) view.findViewById(R.id.tv_set);
		tv_sign = (LinearLayout) view.findViewById(R.id.tv_ewm);
		iv_user = (RoundedImageView) view.findViewById(R.id.imageView1);
		tv_close = (TextView) view.findViewById(R.id.tv_close);
		tv_good = (TextView) view.findViewById(R.id.tv_good);
		tv_no = (TextView) view.findViewById(R.id.tv_no);
		tv_name = (TextView) view.findViewById(R.id.textView2);
		relativeLayout2 = (RelativeLayout) view
				.findViewById(R.id.my_flower_layout);
		relativeLayout1 = (RelativeLayout) view
				.findViewById(R.id.my_state_layout);
		getUser();
		options = new DisplayImageOptions.Builder()
				.showImageOnLoading(R.drawable.loading_img)
				.showImageOnFail(R.drawable.list_default_icon)
				.showImageForEmptyUri(R.drawable.list_default_icon)
				.cacheInMemory(true).cacheOnDisk(true)
				.bitmapConfig(Bitmap.Config.ARGB_8888).build();

	}

	private void getUser() {
		HttpMethod requestMethod = HttpMethod.GET;
		String requestUrl = Constent.URL + Constent.USER_MESSAGE + "?doctorId="
				+ SharedPreferencesDB.getInstance(getActivity()).getDocotorId();
		HttpRequestCallBack<DoctorAction> requestCallBack = new HttpRequestCallBack<DoctorAction>(
				new JsonParser(), DoctorAction.class) {

			@Override
			protected void handleResult(
					HttpResponseInfo<DoctorAction> responseInfo) {
				// TODO Auto-generated method stub
				if (responseInfo.success != null) {
					if (responseInfo.success.success) {
						DoctorBeen doctorBeen = responseInfo.success.data.doctor;
						tv_name.setText(doctorBeen.accountName);
						name = doctorBeen.accountName;
						qr = doctorBeen.qrcode;
						imgurl = doctorBeen.imgUrl;
						tv_close.setText(responseInfo.success.data.numberOfAnsweredQuestions
								+ "");
						tv_no.setText(responseInfo.success.data.numberOfUnansweredQuestions
								+ "");
						tv_good.setText(responseInfo.success.data.favorableRate
								+ "");
						SystemApplication.getInstance().mImageLoad1
								.displayImage(doctorBeen.imgUrl, iv_user,
										options);
						stopService = doctorBeen.stopService;
						String id = doctorBeen.accountId;
						String position = doctorBeen.position;
						SharedPreferencesDB db = SharedPreferencesDB
								.getInstance(getActivity());
						db.setAccountName(name);
						db.setDocotorId(id);
						db.setPosition(position);
						db.putImage(imgurl);
						db.setAcceptQuestion(responseInfo.success.data.stopService);
					}
				}

			}

			@Override
			protected void handleError(HttpException e, String msg) {
				// TODO Auto-generated method stub
				if (msg.equals("HttpHostConnectException")) {
					Toast.makeText(getActivity(), "网络请求失败", Toast.LENGTH_SHORT)
							.show();
				}

				else
					Toast.makeText(getActivity(), msg, Toast.LENGTH_SHORT)
							.show();
			}
		};
		HttpRequestParams params = new HttpRequestParams(requestMethod,
				requestUrl, requestCallBack);
		HttpRequest.send(params);
	}

	public void post() {
		// TODO Auto-generated method stub
		Intent intent = new Intent(getActivity(), MessageActivity.class);
		startActivity(intent);

	}

	@Override
	public void selectItem(int position, String title) {
	}

	@Override
	public void show() {
		// TODO Auto-generated method stub

	}

}
